package net.zoneland.data.dataprofiling.common.service.impl;

import net.zoneland.data.dataprofiling.common.config.Constant;
import net.zoneland.data.dataprofiling.common.domain.JobQuery;
import net.zoneland.data.dataprofiling.common.domain.ScheduleJob;
import net.zoneland.data.dataprofiling.common.domain.TaskEntity;
import net.zoneland.data.dataprofiling.common.quartz.utils.QuartzManager;
import net.zoneland.data.dataprofiling.common.repository.TaskRepository;
import net.zoneland.data.dataprofiling.common.service.JobService;
import net.zoneland.data.dataprofiling.common.utils.PageUtils;
import net.zoneland.data.dataprofiling.common.utils.ScheduleJobUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private QuartzManager quartzManager;

    @Override
    public TaskEntity get(Long id) {
        return taskRepository.getOne(id);
    }

    @Override
    public PageUtils list(int page, int size, JobQuery jobQuery) {
        Pageable pageable = PageRequest.of(page,size);
        Page<TaskEntity> taskEntityPage = taskRepository.findAll(pageable);
        PageUtils pageUtils = new PageUtils(taskEntityPage.getContent(), (int) taskEntityPage.getTotalElements());
        return pageUtils;
    }

    @Override
    public int count(Map<String, Object> map) {
        return 0;
    }

    @Override
    public int save(TaskEntity taskScheduleJob) {
        TaskEntity taskEntity = taskRepository.save(taskScheduleJob);
        if(taskEntity!=null){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int update(TaskEntity taskScheduleJob) {
        taskRepository.saveAndFlush(taskScheduleJob);
        return  1;
    }

    @Override
    public int remove(Long id) {
        try {
            TaskEntity scheduleJob = get(id);
            quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
            taskRepository.deleteById(id);
            return 1;
        } catch (SchedulerException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int batchRemove(Long[] ids) {
        for(Long id:ids){
            remove(id);
        }
        return ids.length;
    }

    @Override
    public void initSchedule() throws SchedulerException {
        // 这里获取任务信息数据
        List<TaskEntity> jobList = taskRepository.findAll();
        for (TaskEntity scheduleJob : jobList) {
            if ("1".equals(scheduleJob.getJobStatus())) {
                ScheduleJob job = ScheduleJobUtils.entityToData(scheduleJob);
                quartzManager.addJob(job);
            }

        }
    }

    @Override
    public void changeStatus(Long jobId, String cmd) throws SchedulerException {
        TaskEntity scheduleJob = get(jobId);
        if (scheduleJob == null) {
            return;
        }
        if (Constant.STATUS_RUNNING_STOP.equals(cmd)) {
            quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
            scheduleJob.setJobStatus(ScheduleJob.STATUS_NOT_RUNNING);
        } else {
            if (!Constant.STATUS_RUNNING_START.equals(cmd)) {
            } else {
                scheduleJob.setJobStatus(ScheduleJob.STATUS_RUNNING);
                quartzManager.addJob(ScheduleJobUtils.entityToData(scheduleJob));
            }
        }
        update(scheduleJob);
    }

    @Override
    public void updateCron(Long jobId) throws SchedulerException {
        TaskEntity scheduleJob = get(jobId);
        if (scheduleJob == null) {
            return;
        }
        if (ScheduleJob.STATUS_RUNNING.equals(scheduleJob.getJobStatus())) {
            quartzManager.updateJobCron(ScheduleJobUtils.entityToData(scheduleJob));
        }
        update(scheduleJob);
    }
}
